package com.lantosec.hmrp.modules.category.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.modules.category.entity.AssetsCheckPlan;
import com.lantosec.hmrp.modules.category.service.IAssetsCheckPlanService;
import com.lantosec.hmrp.modules.category.vo.AssetsCheckPlanVO;
import com.lantosec.hmrp.modules.category.vo.UserInfoVO;
import com.lantosec.hmrp.modules.system.entity.RegionalHead;
import com.lantosec.hmrp.modules.system.vo.RegionalHeadVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 *
 * 盘点计划 控制器
 *
 * @author AutoCode
 * @since 2020-04-01
 */
@Api(value = "盘点计划控制器", tags = "盘点计划控制器")
@RestController
@RequestMapping("/category/checkPlan")
@Log4j2
public class AssetsCheckPlanController extends BaseController {

    @Autowired
    private IAssetsCheckPlanService service;

    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<List<AssetsCheckPlanVO>> all(){
        return Result.ok(entity2VO(service.list()));
    }

    @GetMapping("page")
    @ApiOperation(value = "分页查询")
    public Result<Page<AssetsCheckPlanVO>> page(
            @ApiParam(value = "分页对象", required = true) PageVO<AssetsCheckPlan> page, AssetsCheckPlanVO condition){
        Page<AssetsCheckPlan> infoPage = service.queryPage(page,condition);
        return Result.ok(entity2VO(infoPage));
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "主键查询")
    public Result<AssetsCheckPlanVO> get(@ApiParam(value = "ID", required = true) @PathVariable(name="id") String id){
        return Result.ok(entity2VO(service.getById(id)));
    }

    @PostMapping("create")
    @ApiOperation(value = "新增数据")
    public Result<String> create(
            @ApiParam(value = "盘点计划VO对象", required = true) @RequestBody @Validated Map<String,Object> map, BindingResult bindingResult){
        validationHandler(bindingResult);
        service.create(map);
        return Result.ok();
    }

    @PutMapping("update")
    @ApiOperation(value = "修改数据")
    public Result<Void> update(@ApiParam(value = "盘点计划VO对象", required = true) @RequestBody AssetsCheckPlanVO vo){
        AssetsCheckPlan entity = vo.convertToEntity();
        service.updateById(entity);
        return Result.ok();
    }

    @DeleteMapping(value = "delete/{ids}")
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids){
        if(StringUtils.isNotEmpty(ids)){
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }

    @GetMapping("queryUserList")
    @ApiOperation("查询人员列表")
    public Result<List<UserInfoVO>> queryUserList(){
        List<UserInfoVO> param = service.queryUserList();
        return Result.ok(param);
    }

    @PostMapping("queryName")
    @ApiOperation("根据ID查Name")
    public Result<RegionalHead> queryName(
            @ApiParam(value = "对象ID", required = true) @RequestBody RegionalHeadVO vo){
        RegionalHead entity = vo.convertToEntity();
        RegionalHead param = service.queryName(entity);
        return Result.ok(param);
    }

    @GetMapping("suspend")
    @ApiOperation(value = "中止计划")
    public Result<List> suspend(
            @ApiParam(value = "计划ID") @RequestParam(name = "planId") String planId
    ){
        service.suspend(planId);
        return Result.ok();
    }
}
